package com.bsj.power.common.config.cached;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsj.power.common.config.redis.RedisConstant;
import com.bsj.power.common.netty.ACDeviceStatusData;
import com.bsj.power.common.netty.StockInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import redis.clients.bsj.JedisClusterBSJ;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description BindCardMachineCache2
 * @time 2024/10/21 10:10
 */
@Slf4j
@Component
public class BindCardMachineCache2 {

    @Autowired
    private JedisClusterBSJ jedisClusterBSJ;

    /**
     * 有序
     * KEY绑卡机设备ID
     * VALUE设备状态上报信息
     */
    private static Map<String, ACDeviceStatusData> acDeviceStatusDataMap = new LinkedHashMap<>();
    /**
     * 有序
     * 大KEY绑卡机设备ID
     * 小KEY仓位号
     * VALUE每个仓位的信息
     */
    private static Map<String, Map<Integer, StockInfo>> stockInfoMap = new LinkedHashMap<>();

    /**
     * 添加事件上报信息
     *
     * @param acDeviceStatusData
     * @return void
     * @author ljx
     * @time 2024/9/23 20:14
     */
    public void saddAcDeviceStatusData(ACDeviceStatusData acDeviceStatusData) {
        acDeviceStatusDataMap.put(acDeviceStatusData.getDeviceId(), acDeviceStatusData);
    }

    /**
     * 获取事件上报信息
     *
     * @param deviceId
     * @return com.bsj.power.common.netty.ACData
     * @author ljx
     * @time 2024/9/23 20:23
     */
    public static ACDeviceStatusData getACDeviceStatusData(String deviceId) {
        return acDeviceStatusDataMap.get(deviceId);
    }

    /**
     * 获取所有事件上报信息
     *
     * @return java.util.List<com.bsj.power.common.netty.ACData>
     * @author ljx
     * @time 2024/9/23 20:24
     */
    public static List<ACDeviceStatusData> getAllACDeviceStatusData() {
        return acDeviceStatusDataMap.values().stream().collect(Collectors.toList());
    }

    /**
     * 添加仓位信息集
     *
     * @param acDeviceStatusData
     * @return void
     * @author ljx
     * @time 2024/9/24 20:47
     */
    public void hsetAllStockInfo(ACDeviceStatusData acDeviceStatusData) {
        if (acDeviceStatusData == null) {
            return;
        }
        String deviceId = acDeviceStatusData.getDeviceId();
        if (StringUtils.isBlank(deviceId)) {
            return;
        }
        List<StockInfo> stockInfoList = acDeviceStatusData.getStockInfos();
        if (CollectionUtils.isEmpty(stockInfoList)) {
            return;
        }
        for (StockInfo stockInfo : stockInfoList) {
            jedisClusterBSJ.hset(RedisConstant.BIND_CARD_STOCK_DATA + deviceId, stockInfo.getSerialNum() + "", JSON.toJSONString(stockInfo));
        }
        log.info("添加完后仓位信息：{}", jedisClusterBSJ.hgetAll(RedisConstant.BIND_CARD_STOCK_DATA + deviceId));
    }

    /**
     * 添加单个仓位信息
     *
     * @param deviceId
     * @param stockInfo
     * @return void
     * @author ljx
     * @time 2024/9/24 20:46
     */
    public void hsetStockInfo(String deviceId, StockInfo stockInfo) {
        if (StringUtils.isBlank(deviceId) || stockInfo == null || stockInfo.getSerialNum() == null) {
            return;
        }
        jedisClusterBSJ.hset(RedisConstant.BIND_CARD_STOCK_DATA + deviceId, stockInfo.getSerialNum() + "", JSON.toJSONString(stockInfo));
    }

    /**
     * 获取单个仓位信息
     *
     * @param deviceId
     * @param serialNum
     * @return com.bsj.power.common.netty.StockInfo
     * @author ljx
     * @time 2024/9/24 20:46
     */
    public StockInfo hgetStockInfo(String deviceId, Integer serialNum) {
        if (StringUtils.isBlank(deviceId) || serialNum == null) {
            return null;
        }
        String str = jedisClusterBSJ.hget(RedisConstant.BIND_CARD_STOCK_DATA + deviceId, serialNum + "");
        if (StringUtils.isBlank(str)) {
            return null;
        }
        return JSONObject.parseObject(str, StockInfo.class);
    }

    /**
     * 获取某个绑卡机所有仓位信息
     *
     * @param deviceId
     * @return java.util.List<com.bsj.power.common.netty.StockInfo>
     * @author ljx
     * @time 2024/9/24 20:46
     */
    public List<StockInfo> hgetAllStockInfo(String deviceId) {
        if (StringUtils.isBlank(deviceId)) {
            return null;
        }
        Map<String, String> map = jedisClusterBSJ.hgetAll(RedisConstant.BIND_CARD_STOCK_DATA + deviceId);
        if (CollectionUtils.isEmpty(map)) {
            return null;
        }
        return map.values().stream().map(str -> {
            return JSONObject.parseObject(str, StockInfo.class);
        }).collect(Collectors.toList());
    }

}
